package cms.simmytech.web.major;
import cms.simmytech.model.TbMajor;
import cms.simmytech.service.TbMajorService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/major")
public class MajorController extends BaseController {

    @Autowired
    private TbMajorService service;
    //专业列表
    @RequestMapping(value = "/majorList")
    public String majorList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model,String name,Byte majorType) {
        PageInfo<TbMajor> pageInfo = service.getMajorList(pageNum, pageSize,name,majorType);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("majorType", majorType);
        return super.toPage("major/majorList", model);
    }

    //展示或填写专业信息
    @RequestMapping("/majorDetail")
    public String majorDetail(Integer MajorId,Model model){//用来显示一个专业的详细信息
        TbMajor major=null;
        if(MajorId==null){//当专业id为空时，显示空白详情页，用来新增专业信息
            major=new TbMajor();
        }else{//当专业id非空时，查询专业信息，用来修改专业信息
            major=service.findMajorById(MajorId);
        }
        model.addAttribute("major",major);
        return super.toPage("major/majorDetail",model);//以后返回页面，尽量用return super.toPage()
        //return "major/majorDetail";//这种写法，在下个页面，无法引用css文件
    }

    //保存或者更新专业信息
    @PostMapping(value = "/majorSave")
    public String majorSave(TbMajor  major , Model model) {
        service.saveMajor(major);
        return "redirect:majorList";
    }


    @RequestMapping(value = "/checkCode")
    @ResponseBody
    public Map<String, Object> checkUserName(Integer id, String code, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("res", service.checkCode(id, code,type));
        return map;
    }
    //删除专业
    @ResponseBody
    @GetMapping("/deleteMajorById")
    public Boolean deleteMajorById(Integer MajorId){
        return service.deleteMajorById(MajorId);
    }
    //写给微信小程序的接口，直接返回json格式数据
    //查询所有专业的信息
    @ResponseBody
    @GetMapping("/findAllMajor")
    public List<TbMajor> findAllMajor(){
        return service.findAllMajor();
    }

    //查找一个专业
    //http://localhost:8081/icecCms/findMajorById?MajorId=4
    @ResponseBody
    @GetMapping("/findMajorById")
    public TbMajor findMajorById(Integer MajorId){//MajorId是通过接口地址传递的参数
        return service.findMajorById(MajorId);
    }

    //通过专业名称进行模糊查询，返回专业列表
    @RequestMapping("/getMajorListByNameSearch")
    public String getMajorListByNameSearch(String majorName,Model model){
        PageInfo<TbMajor> pageInfo = service.getMajorListByName(majorName);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("majorNameKeyword",majorName);//majorNameKeyword这是搜索框的关键词,搜索后，在前端回显关键词
        return super.toPage("major/majorList",model);//以后返回页面，尽量用return super.toPage()
    }
    //修改专业
    @PostMapping("/updateMajor")//提交信息用post请求
    public String updateMajor(Integer majorId, String majorName,String majorDesc,Integer startTime,Byte majorType, Model model){//@RequestBody把请求里的json转换成Java对象
        TbMajor major=new TbMajor();
        major.setMajorId(majorId);
        //System.out.println(majorId+"  "+majorName+"  "+majorDesc);
        major.setComId(0);
        major.setFatherId(0);
        major.setMajorName(majorName);
        major.setMajorDesc(majorDesc);
        major.setStartTime(startTime);
        major.setMajorState(new Byte("1"));
        major.setMajorType(majorType);
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        major.setRecTime(timeStamp);
        service.updataMajor(major);
        return "redirect:majorList";
    }

}
